#include "stdafx.h"
#include "SkillManager.h"

SkillManager::SkillManager()
{
	//loadItemCSV();
}
SkillManager::~SkillManager()
{

}

Skill* SkillManager::Add(SkillId id)
{
	Skill* pSkill = manager<Skill,SkillId>::Add(id);
	LOGGER_INFO("Add,pSkill:%p,id:%d",pSkill,id);
	return pSkill;
}
bool SkillManager::Remove(SkillId id)
{
	if (id <= 0) return false;
	LOGGER_INFO("Remove,id:%d",id);
	return manager<Skill,SkillId>::Remove(id);
}
void SkillManager::loadSkillCSV()
{
	if(m_mapSkillCSV.size() != 0)
		return;
	std::string path = singleton<GameServer>::instance().GetServerPath();
	std::string filename = path + "csv/skill.csv";
#ifndef _WIN32
	filename = "./csv/skill.csv";
#endif
	m_csvReader.Load(filename);

	for (Int32 i = 0; i < m_csvReader.GetRowCount(); i++)
	{
		Int32 id = m_csvReader.GetInt(i,"id");
		if(m_mapSkillCSV.find(id) != m_mapSkillCSV.end())
		{
			LOGGER_ERROR("m_mapSkillCSV.find(id) != m_mapSkillCSV.end(),id:%d",id);
			return;
		}

		m_mapSkillCSV[id].id = id;
		SkillCSVInfo& info = m_mapSkillCSV[id];

		info.type = m_csvReader.GetInt(i,"type");
		info.name = m_csvReader.GetString(i,"name");
		info.kind = m_csvReader.GetString(i,"kind");
		info.description = m_csvReader.GetString(i,"description");
		info.icon_id = m_csvReader.GetInt(i,"icon_id");
		info.require_level = m_csvReader.GetInt(i,"require_level");
		info.require_gender = m_csvReader.GetInt(i,"require_gender");

		info.action_self = m_csvReader.GetInt(i,"action_self");
		info.action_target = m_csvReader.GetInt(i,"action_target");
		info.effect_self = m_csvReader.GetInt(i,"effect_self");
		info.effect_self_fullscreen = m_csvReader.GetInt(i,"effect_self_fullscreen");
		info.effect_target_attacked = m_csvReader.GetInt(i,"effect_target_attacked");
		info.effect_target_hit = m_csvReader.GetInt(i,"effect_target_hit");
	}
}
SkillCSVInfo* SkillManager::GetSkillCSVInfo(Int32 csv_id)
{
	std::map<Int32,SkillCSVInfo>::iterator iter = m_mapSkillCSV.find(csv_id);
	if (iter == m_mapSkillCSV.end())
	{
		LOGGER_ERROR("GetSkillCSVInfo,iter == m_mapSkillCSV.end(),csv_id:%d",csv_id);
		return NULL;
	}
	return &(iter->second);
}
Skill* SkillManager::CreateFromSkillInfo(SkillInfo& info)
{
	SkillCSVInfo* pSkillCSVInfo = GetSkillCSVInfo(info.csv_id);
	if (pSkillCSVInfo == NULL)
	{
		LOGGER_ERROR("CreateFromSkillInfo,pSkillCSVInfo == NULL,info.csv_id:%d",info.csv_id);
		return NULL;
	}
	Int32 nSkillId = GenerateIdInt32();
	Skill* pSkill = Add(nSkillId);
	if (pSkill == NULL)
	{
		LOGGER_ERROR("CreateFromSkillInfo,pSkill == NULL,nSkillId:%d",nSkillId);
		return NULL;
	}
	pSkill->SetSkillInfo(info);
	pSkill->SetSkillCSVInfo(pSkillCSVInfo);
	return pSkill;
}
Skill* SkillManager::CreateFromCSVId(Int32 csv_id)
{
	SkillCSVInfo* pSkillCSVInfo = GetSkillCSVInfo(csv_id);
	if (pSkillCSVInfo == NULL)
	{
		LOGGER_ERROR("CreateFromCSVId,pSkillCSVInfo == NULL,csv_id:%d",csv_id);
		return NULL;
	}
	Int32 nSkillId = GenerateIdInt32();
	Skill* pSkill= Add(nSkillId);
	if (pSkill == NULL)
	{
		LOGGER_ERROR("CreateFromCSVId,pSkill== NULL,nSkillId:%d",nSkillId);
		return NULL;
	}
	SkillInfo info;
	GenerateSkillInfo(*pSkillCSVInfo,info);
	pSkill->SetSkillInfo(info);
	pSkill->SetSkillCSVInfo(pSkillCSVInfo);
	return pSkill;

}

void SkillManager::GenerateSkillInfo(SkillCSVInfo& csv_info,SkillInfo &info)
{
	info.csv_id = csv_info.id;
	info.level = 0;
}
